package com.booter.datasource;

import com.bstek.ureport.definition.datasource.BuildinDatasource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.sqlite.SQLiteDataSource;

import javax.annotation.PostConstruct;
import java.io.File;
import java.sql.Connection;

/**
 * @author xueqiu
 * @date 2023/3/31 18:00
 */
@Slf4j
@Component
public class SqliteDataSource implements BuildinDatasource {

    private SQLiteDataSource ds;

    @Value("${sqlite.url}")
    private String url;

    @PostConstruct
    public void init() {
        File file = new File("");
        ds = new SQLiteDataSource();
        ds.setUrl("jdbc:sqlite:" + file.getAbsolutePath().concat(File.separator).concat(url));
    }


    @Override
    public String name() {
        return "sqlite数据源";
    }

    @SneakyThrows
    @Override
    public Connection getConnection() {
        return ds.getConnection();
    }
}
